<!--
 * @Author: liyin 2179253556@qq.com
 * @Date: 2025-07-01 08:39:53
 * @LastEditors: liyin 2179253556@qq.com
 * @LastEditTime: 2025-07-30 09:38:40
 * @FilePath: \school-based-courses-admin\src\components\Course\Subject\EditModify.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <Model :title="ruleForm.id ? '编辑公告' : '添加公告'" :modelShow="props.modelShow"
            @cancel="Cancel(ruleFormRef)" @confirm="Confirm(ruleFormRef)">
            <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="80px" status-icon label-position="top">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="公告内容" prop="content">
                            <el-input type="textarea" v-model="ruleForm.content" :rows="5" placeholder="请输入公告内容"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </Model>
    </div>
</template>
<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, onMounted, reactive, watch, nextTick } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { PiniaStore } from '../../../store'
import { addNotice, editNotice } from '../../Api/Publish'
const store = PiniaStore()
interface PropsType {
    modelShow?: boolean
}
const props = withDefaults(defineProps<PropsType>(), {
    modelShow: false,
})
const ruleForm = reactive<any>({
    uuid: '',
    content: '',
})
interface EmitType {
    (e: 'modelCancel', bol: boolean): void
    (e: 'getList'): void
}
const emit = defineEmits<EmitType>()
const ruleFormRef = ref<FormInstance>()
const rules = reactive<FormRules>({
    content: [{ required: true, message: '请输入公告内容', trigger: 'blur' }],
})
const Cancel = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    emit('modelCancel', false)
}
// 点击确定编辑或者新增
const Confirm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid: any) => {
        if (valid) {
            const obj: any = {
                content: ruleForm.content
            }
            if (ruleForm.uuid) {
                obj.id = ruleForm.uuid
            }
            const res: any = ruleForm.uuid ? await editNotice(obj) : await addNotice(obj)
            if (res.code !== 1) return ElMessage.error(res.msg)
            ElMessage.success(ruleForm.uuid?'添加成功':'更新成功')
            emit('modelCancel', false)
            formEl.resetFields()
            emit('getList')
        } else {
            console.log('error submit!')
        }
    })
}
onMounted(() => {
})
defineExpose({ ruleForm })
</script>
<style lang="scss" scoped>
</style>
